//
//  GBInterMaster_APPLogic.m
//  NetworkTest
//
//  Created by xiaosi on 16/7/26.
//  Copyright © 2016年 xiaosi. All rights reserved.
//

#import "GBInterMaster_APPLogic.h"

@implementation GBInterMaster_APPLogic

/* 连接注册 */
+ (int) connRegister:(GBInterMaster_Launcher*)service{
    NSLog(@"连接注册");
    GBInterMaster_NetData* packData = [[GBInterMaster_NetData alloc] init];
    packData.cmdType = PTCP_V10_VALUE_CMD_TYPE_APP_SYSTEM;
    packData.cmd = LOGIC_APP_CMD_CONNREGISTER;
    packData.msgId = 1;
    Byte reqs[10];
    memset(reqs, 0x00, 10);
    reqs[0] = SYSTEM_VERSIONS & 0x00FF;
    reqs[1] = ((SYSTEM_VERSIONS >> 8) & 0x00FF);
    reqs[2] = SYSTEM_VERSIONS & 0x00FF;
    reqs[3] = ((SYSTEM_VERSIONS >> 8) & 0x00FF);
    reqs[4] = SYSTEM_TYPE & 0x00FF;
    reqs[5] = ((SYSTEM_TYPE >> 8) & 0x00FF);
    
    packData.bodyData = [[NSData alloc] initWithBytes:reqs length:10];
    PTCP res = [GBInterMaster_PackUtil pack10:packData];
    if(res == PTCP_OK){
        [service.socket writeData:packData.packData withTimeout:SEND_MSG_OVER_TIME tag:1];
//        [packData release];
//        [packData.bodyData release];
    }
    return 0;
}
/* 连接注册反馈 */
+ (int) responseConnRegister:(GBInterMaster_NetData*)netData withService:(GBInterMaster_Launcher*)service{
    if (netData.bodyData.length != 28) {
        return 0x01;
    }
    int offset = 0;
    Byte* bytes = (Byte*)[netData.bodyData bytes];
    int result = bytes[offset];
    offset += 1;
    if (result == 0x00) {
        long tm = (long) ((bytes[offset] & 0x00FF)
                          + ((bytes[offset + 1] & 0x00FF) << 8 & 0xFF00)
                          + ((bytes[offset + 2] & 0x00FF) << 16 & 0xFF0000) + (((long)bytes[offset + 3] & 0x00FF) << 24 & 0xFF000000));
        offset += 4;
        int scid = (bytes[offset] & 0x00FF) + ((bytes[offset + 1] & 0x00FF) << 8 & 0xFF00);
        offset += 2;
        service.serverConnID = scid;
        service.aesKey = [[NSData alloc] initWithBytes:bytes+offset length:16];
        offset += 16;
        service.registerTag = true;
        int version = (bytes[offset] & 0x00FF) + ((bytes[offset + 1] & 0x00FF) << 8 & 0xFF00);
        offset += 2;
        //        Intent itt = new Intent(
        //                                DataDictionary.BROADRECEIVE_ACTION_NETWORK_WELCOME_RESPONSE);
        //        if (NetworkService.SYSTEM_VERSIONS != version) {
        //            itt.putExtra("isUpdate", true);
        //            itt.putExtra("upVersions", version);
        //        } else {
        //            itt.putExtra("isUpdate", false);
        //        }
        //        LocalBroadcastManager.getInstance(service).sendBroadcast(itt);
        NSLog(@"注册成功！");
    } else {
        NSLog(@"注册失败！");
        service.registerTag = false;
        return result;
    }
    return result;
}


/* 用户注册（获取验证码）*/
+ (int) requestUserRegisterVegister:(int)msgNumber withAccount:(NSString*)account withService:(GBInterMaster_Launcher*)service{
    NSLog(@"用户注册（获取验证码）");
    NSData* dt = [GBInterMaster_Util AES128Encrypt:account withKey:service.aesKey];
    if(dt == nil){
        return -1;
    }
    unsigned long len = [dt length];
    GBInterMaster_NetData* packData = [[GBInterMaster_NetData alloc] init];
    packData.cmdType = PTCP_V10_VALUE_CMD_TYPE_APP_SYSTEM;
    packData.cmd = LOGIC_APP_CMD_USERREGISTERVEGISTER;
    packData.connId = service.serverConnID;
    packData.msgId = msgNumber;
    Byte reqs[len + 1];
    memset(reqs, 0x00, len + 1);
    reqs[0] = len;
    memcpy(reqs + 1, [dt bytes] , len);
    packData.bodyData = [[NSData alloc] initWithBytes:reqs length:len + 1];
    PTCP res = [GBInterMaster_PackUtil pack10:packData];
    if(res == PTCP_OK){
        [service.socket writeData:packData.packData withTimeout:SEND_MSG_OVER_TIME tag:2];
    }
    return 0;
}
/* 用户注册反馈（获取验证码）*/
+ (int) responseUserRegisterVegister:(GBInterMaster_NetData*)netData withService:(GBInterMaster_Launcher*)service{
    if (netData.bodyData.length != 1) {
        return 0x01;
    }
    Byte* bytes = (Byte*)[netData.bodyData bytes];
    int result = bytes[0];
    NSDictionary* userData = @{@"msgNumber":[NSNumber numberWithInt:netData.msgId],@"responseCode":[NSNumber numberWithInt:result]};
    [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_RESPONSE_REGISTER_VEGISTER object:nil userInfo:userData];
    return result;
}


/* 用户注册 */
+ (int) requestUserRegister:(int)msgNumber withPassword:(NSString*)password withCheckCode:(NSString*)checkCode withService:(GBInterMaster_Launcher*)service{
    NSData* password_dt = [GBInterMaster_Util AES128Encrypt:password withKey:service.aesKey];
    if(password_dt == nil){
        return -1;
    }
    NSData* check_dt = [checkCode dataUsingEncoding:NSUTF8StringEncoding];
    GBInterMaster_NetData* packData = [[GBInterMaster_NetData alloc] init];
    packData.cmdType = PTCP_V10_VALUE_CMD_TYPE_APP_SYSTEM;
    packData.cmd = LOGIC_APP_CMD_USERREGISTER;
    packData.connId = service.serverConnID;
    packData.msgId = msgNumber;
    Byte reqs[22];
    memset(reqs, 0x00, 22);
    memcpy(reqs, [password_dt bytes] , password_dt.length < 16 ? password_dt.length:16);
    memcpy(reqs+16, [check_dt bytes] , 6);
    packData.bodyData = [[NSData alloc] initWithBytes:reqs length:22];
    PTCP res = [GBInterMaster_PackUtil pack10:packData];
    if(res == PTCP_OK){
        [service.socket writeData:packData.packData withTimeout:SEND_MSG_OVER_TIME tag:3];
    }
    return 0;
}
/* 用户注册反馈 */
+ (int) responseUserRegister:(GBInterMaster_NetData*)netData withService:(GBInterMaster_Launcher*)service{
    if (netData.bodyData.length != 1) {
        return 0x01;
    }
    Byte* bytes = (Byte*)[netData.bodyData bytes];
    int result = bytes[0];
    NSDictionary* userData = @{@"msgNumber":[NSNumber numberWithInt:netData.msgId],@"responseCode":[NSNumber numberWithInt:result]};
    [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_RESPONSE_REGISTER object:nil userInfo:userData];
    return result;
}


/* 忘记密码（获取校验码）*/
+ (int) requestUserForgetPwdVegister:(int)msgNumber withAccount:(NSString*)account withService:(GBInterMaster_Launcher*)service{
    NSData* dt = [GBInterMaster_Util AES128Encrypt:account withKey:service.aesKey];
    if(dt == nil){
        return -1;
    }
    unsigned long len = [dt length];
    GBInterMaster_NetData* packData = [[GBInterMaster_NetData alloc] init];
    packData.cmdType = PTCP_V10_VALUE_CMD_TYPE_APP_SYSTEM;
    packData.cmd = LOGIC_APP_CMD_RESETPWDVEGISTER;
    packData.connId = service.serverConnID;
    packData.msgId = msgNumber;
    Byte reqs[len + 1];
    memset(reqs, 0x00, len + 1);
    reqs[0] = len;
    memcpy(reqs + 1, [dt bytes] , len);
    packData.bodyData = [[NSData alloc] initWithBytes:reqs length:len + 1];
    PTCP res = [GBInterMaster_PackUtil pack10:packData];
    if(res == PTCP_OK){
        [service.socket writeData:packData.packData withTimeout:SEND_MSG_OVER_TIME tag:4];
    }
    return 0;
}
/* 忘记密码反馈 （获取校验码） */
+ (int) responseUserForgetPwdVegister:(GBInterMaster_NetData*)netData withService:(GBInterMaster_Launcher*)service{
    if (netData.bodyData.length != 1) {
        return 0x01;
    }
    Byte* bytes = (Byte*)[netData.bodyData bytes];
    int result = bytes[0];
    NSDictionary* userData = @{@"msgNumber":[NSNumber numberWithInt:netData.msgId],@"responseCode":[NSNumber numberWithInt:result]};
    [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_RESPONSE_FORGETPWD_VEGISTER object:nil userInfo:userData];
    return result;
}


/* 忘记密码 */
+ (int) requestUserForgetPwd:(int)msgNumber  withCheckCode:(NSString*)checkCode withService:(GBInterMaster_Launcher*)service{
    NSData* check_dt = [checkCode dataUsingEncoding:NSUTF8StringEncoding];
    GBInterMaster_NetData* packData = [[GBInterMaster_NetData alloc] init];
    packData.cmdType = PTCP_V10_VALUE_CMD_TYPE_APP_SYSTEM;
    packData.cmd = LOGIC_APP_CMD_RESETPWD;
    packData.connId = service.serverConnID;
    packData.msgId = msgNumber;
    Byte reqs[6];
    memset(reqs, 0x00, 6);
    memcpy(reqs, [check_dt bytes] , 6);
    packData.bodyData = [[NSData alloc] initWithBytes:reqs length:6];
    PTCP res = [GBInterMaster_PackUtil pack10:packData];
    if(res == PTCP_OK){
        [service.socket writeData:packData.packData withTimeout:SEND_MSG_OVER_TIME tag:5];
    }
    return 0;
}
/* 忘记密码反馈 */
+ (int) responseUserForgetPwd:(GBInterMaster_NetData*)netData withService:(GBInterMaster_Launcher*)service{
    if (netData.bodyData.length != 1) {
        return 0x01;
    }
    Byte* bytes = (Byte*)[netData.bodyData bytes];
    int result = bytes[0];
    NSDictionary* userData = @{@"msgNumber":[NSNumber numberWithInt:netData.msgId],@"responseCode":[NSNumber numberWithInt:result]};
    [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_RESPONSE_FORGETPWD_VERIFY object:nil userInfo:userData];
    return result;
}


/* 忘记密码（设置新密码） */
+ (int) requestUserNewPwd:(int)msgNumber withPassword:(NSString*)password withService:(GBInterMaster_Launcher*)service{
    NSData* password_dt = [GBInterMaster_Util AES128Encrypt:password withKey:service.aesKey];
    if(password_dt == nil){
        return -1;
    }
    GBInterMaster_NetData* packData = [[GBInterMaster_NetData alloc] init];
    packData.cmdType = PTCP_V10_VALUE_CMD_TYPE_APP_SYSTEM;
    packData.cmd = LOGIC_APP_CMD_RESETPWDNEW;
    packData.connId = service.serverConnID;
    packData.msgId = msgNumber;
    Byte reqs[16];
    memset(reqs, 0x00, 16);
    memcpy(reqs, [password_dt bytes] , password_dt.length < 16 ? password_dt.length:16);
    packData.bodyData = [[NSData alloc] initWithBytes:reqs length:16];
    PTCP res = [GBInterMaster_PackUtil pack10:packData];
    if(res == PTCP_OK){
        [service.socket writeData:packData.packData withTimeout:SEND_MSG_OVER_TIME tag:6];
    }
    return 0;
}
/* 忘记密码反馈（设置新密码） */
+ (int) responseUserNewPwd:(GBInterMaster_NetData*)netData withService:(GBInterMaster_Launcher*)service{
    if (netData.bodyData.length != 1) {
        return 0x01;
    }
    Byte* bytes = (Byte*)[netData.bodyData bytes];
    int result = bytes[0];
    NSDictionary* userData = @{@"msgNumber":[NSNumber numberWithInt:netData.msgId],@"responseCode":[NSNumber numberWithInt:result]};
    [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_RESPONSE_FORGETPWD_NEWPWD object:nil userInfo:userData];
    return result;
}


/* 用户登录 */
+ (int) requestLogin:(int)msgNumber withUserInfo:(GBInterMaster_Bean_UserInfo*)user withService:(GBInterMaster_Launcher*)service{
    NSData* account_dt = [GBInterMaster_Util AES128Encrypt:user.account withKey:service.aesKey];
    if(account_dt == nil){
        return -1;
    }
    unsigned long len = [account_dt length];
    NSData* password_dt = [GBInterMaster_Util AES128Encrypt:user.loginPassword withKey:service.aesKey];
    if(password_dt == nil){
        return -1;
    }
    GBInterMaster_NetData* packData = [[GBInterMaster_NetData alloc] init];
    packData.cmdType = PTCP_V10_VALUE_CMD_TYPE_APP_SYSTEM;
    packData.cmd = LOGIC_APP_CMD_LOGIN;
    packData.connId = service.serverConnID;
    packData.msgId = msgNumber;
    if(user.loginCheck){
        Byte reqs[len+23];
        memset(reqs, 0x00, len+23);
        reqs[0] = len;
        memcpy(reqs+1, [account_dt bytes] ,len );
        memcpy(reqs+len+1, [password_dt bytes] ,password_dt.length < 16 ? password_dt.length:16 );
        NSData* check_dt = [user.checkCode dataUsingEncoding:NSUTF8StringEncoding];
        memcpy(reqs+len+17, [check_dt bytes] ,6);
        packData.bodyData = [[NSData alloc] initWithBytes:reqs length:len+23];
    }else{
        Byte reqs[len+17];
        memset(reqs, 0x00, len+17);
        reqs[0] = len;
        memcpy(reqs+1, [account_dt bytes] ,len );
        memcpy(reqs+len+1, [password_dt bytes] ,password_dt.length < 16 ? password_dt.length:16 );
        packData.bodyData = [[NSData alloc] initWithBytes:reqs length:len+17];
    }
    PTCP res = [GBInterMaster_PackUtil pack10:packData];
    if(res == PTCP_OK){
        [service.socket writeData:packData.packData withTimeout:SEND_MSG_OVER_TIME tag:7];
    }
    return 0;
}
/* 用户登录反馈 */
+ (int) responseLogin:(GBInterMaster_NetData*)netData withService:(GBInterMaster_Launcher*)service{
    NSDictionary* userData;
    Byte* bytes = (Byte*)[netData.bodyData bytes];
    int result = bytes[0];
    if (netData.bodyData.length == 1) {
        if(result == 0x00){
            service.loginUser.loginCheck = false;
        }
        userData = @{@"msgNumber":[NSNumber numberWithInt:netData.msgId],@"responseCode":[NSNumber numberWithInt:result],@"isCheck":[NSNumber numberWithInt:0x00]};
    }else if (netData.bodyData.length == 7) {
        service.loginUser.loginCheck = true;
        NSString* checkCode = [[NSString alloc] initWithBytes:bytes+1 length:6 encoding:NSUTF8StringEncoding];
        userData = @{@"msgNumber":[NSNumber numberWithInt:netData.msgId],@"responseCode":[NSNumber numberWithInt:result],@"isCheck":[NSNumber numberWithInt:0x01],@"checkCode":checkCode};
    }else{
        return result;
    }
    NSLog(@"登录结果：%x",result);
    [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_RESPONSE_LOGIN object:nil userInfo:userData];
    return result;
}


/* 修改密码 */
+ (int) requestUpdatePwd:(int)msgNumber withOldPassword:(NSString*)oldPassword withNewPassword:(NSString*)newPassword withService:(GBInterMaster_Launcher*)service{
    NSData* old_dt = [GBInterMaster_Util AES128Encrypt:oldPassword withKey:service.aesKey];
    if(old_dt == nil){
        return -1;
    }
    NSData* new_dt = [GBInterMaster_Util AES128Encrypt:newPassword withKey:service.aesKey];
    if(new_dt == nil){
        return -1;
    }
    GBInterMaster_NetData* packData = [[GBInterMaster_NetData alloc] init];
    packData.cmdType = PTCP_V10_VALUE_CMD_TYPE_APP_SYSTEM;
    packData.cmd = LOGIC_APP_CMD_UPDATEPWD;
    packData.connId = service.serverConnID;
    packData.msgId = msgNumber;
    Byte reqs[32];
    memset(reqs, 0x00, 32);
    memcpy(reqs, [old_dt bytes] ,old_dt.length < 16 ? old_dt.length:16 );
    memcpy(reqs+16, [new_dt bytes] ,new_dt.length < 16 ? new_dt.length:16 );
    packData.bodyData = [[NSData alloc] initWithBytes:reqs length:32];
    PTCP res = [GBInterMaster_PackUtil pack10:packData];
    if(res == PTCP_OK){
        [service.socket writeData:packData.packData withTimeout:SEND_MSG_OVER_TIME tag:8];
    }
    return 0;
}
/* 修改密码反馈 */
+ (int) responseUpdatePwd:(GBInterMaster_NetData*)netData withService:(GBInterMaster_Launcher*)service{
    if (netData.bodyData.length != 1) {
        return 0x01;
    }
    Byte* bytes = (Byte*)[netData.bodyData bytes];
    int result = bytes[0];
    NSDictionary* userData = @{@"msgNumber":[NSNumber numberWithInt:netData.msgId],@"responseCode":[NSNumber numberWithInt:result]};
    [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_RESPONSE_UPDATEPWD object:nil userInfo:userData];
    return result;
}


/* 所有者认证 */
+ (int) requestAcceptPossessor:(int)msgNumber withDeviceKey:(NSString*)deviceKey withService:(GBInterMaster_Launcher*)service{
    NSData* key_dt = [GBInterMaster_Util AES128Encrypt:deviceKey withKey:service.aesKey];
    if(key_dt == nil){
        return -1;
    }
    GBInterMaster_NetData* packData = [[GBInterMaster_NetData alloc] init];
    packData.cmdType = PTCP_V10_VALUE_CMD_TYPE_APP_SYSTEM;
    packData.cmd = LOGIC_APP_CMD_ACCEPTPOSSESSOR;
    packData.connId = service.serverConnID;
    packData.msgId = msgNumber;
    Byte reqs[32];
    memset(reqs, 0x00, 32);
    memcpy(reqs, [key_dt bytes] ,key_dt.length < 32 ? key_dt.length:32 );
    packData.bodyData = [[NSData alloc] initWithBytes:reqs length:32];
    PTCP res = [GBInterMaster_PackUtil pack10:packData];
    if(res == PTCP_OK){
        [service.socket writeData:packData.packData withTimeout:SEND_MSG_OVER_TIME tag:9];
    }
    return 0;
}
/* 所有者认证反馈 */
+ (int) responseAcceptPossessor:(GBInterMaster_NetData*)netData withService:(GBInterMaster_Launcher*)service{
    if (netData.bodyData.length != 1) {
        return 0x01;
    }
    Byte* bytes = (Byte*)[netData.bodyData bytes];
    int result = bytes[0];
    NSDictionary* userData = @{@"msgNumber":[NSNumber numberWithInt:netData.msgId],@"responseCode":[NSNumber numberWithInt:result]};
    [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_RESPONSE_POSSESSOR object:nil userInfo:userData];
    return result;
}


/* 修改设备信息 */
+ (int) requestDeviceRename:(int)msgNumber withDeviceAccount:(NSString*)deviceAccount withDeviceName:(NSString*)deviceName withService:(GBInterMaster_Launcher*)service{
    NSData* account_dt = [deviceAccount dataUsingEncoding:NSUTF8StringEncoding];
    NSData* name_dt = [deviceName dataUsingEncoding:NSUTF8StringEncoding];
    unsigned long len = [name_dt length];
    GBInterMaster_NetData* packData = [[GBInterMaster_NetData alloc] init];
    packData.cmdType = PTCP_V10_VALUE_CMD_TYPE_APP_SYSTEM;
    packData.cmd = LOGIC_APP_CMD_UPDATEDEVICEINFO;
    packData.connId = service.serverConnID;
    packData.msgId = msgNumber;
    Byte reqs[len + 17];
    memset(reqs, 0x00, len + 17);
    memcpy(reqs, [account_dt bytes] , account_dt.length < 16 ? account_dt.length:16);
    reqs[16] = len;
    memcpy(reqs+17, [name_dt bytes] , len);
    packData.bodyData = [[NSData alloc] initWithBytes:reqs length:len + 17];
    PTCP res = [GBInterMaster_PackUtil pack10:packData];
    if(res == PTCP_OK){
        [service.socket writeData:packData.packData withTimeout:SEND_MSG_OVER_TIME tag:10];
    }
    return 0;
}
/* 修改设备信息反馈 */
+ (int) responseDeviceRename:(GBInterMaster_NetData*)netData withService:(GBInterMaster_Launcher*)service{
    if (netData.bodyData.length < 17) {
        return 0x01;
    }
    Byte* bytes = (Byte*)[netData.bodyData bytes];
    int offset = 0;
    int result = bytes[offset];
    offset += 1;
    NSString* deviceAccount = [[NSString alloc] initWithBytes:bytes+offset length:16 encoding:NSUTF8StringEncoding];
    offset += 16;
    int deviceAliasLen = bytes[offset];
    offset += 1;
    NSString* deviceAlias = [[NSString alloc] initWithBytes:bytes+offset length:deviceAliasLen encoding:NSUTF8StringEncoding];
    NSDictionary* userData = @{@"msgNumber":[NSNumber numberWithInt:netData.msgId],@"responseCode":[NSNumber numberWithInt:result],@"deviceAliasLen":deviceAlias};
    [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_RESPONSE_DEVICE_RENAME object:nil userInfo:userData];
    return result;
}


/* 获取设备列表 */
+ (int) requestDeviceList:(int)msgNumber withService:(GBInterMaster_Launcher*)service{
    GBInterMaster_NetData* packData = [[GBInterMaster_NetData alloc] init];
    packData.cmdType = PTCP_V10_VALUE_CMD_TYPE_APP_SYSTEM;
    packData.cmd = LOGIC_APP_CMD_GETDEVICELIS;
    packData.connId = service.serverConnID;
    packData.msgId = msgNumber;
    Byte reqs[1];
    reqs[0] = 0x01;
    packData.bodyData = [[NSData alloc] initWithBytes:reqs length:1];
    PTCP res = [GBInterMaster_PackUtil pack10:packData];
    if(res == PTCP_OK){
        [service.socket writeData:packData.packData withTimeout:SEND_MSG_OVER_TIME tag:11];
    }
    return 0;
}
/* 获取设备列表反馈 */
+ (int) responseDeviceList:(GBInterMaster_NetData*)netData withService:(GBInterMaster_Launcher*)service{
    if (netData.bodyData.length < 1) {
        return 0x01;
    }
    bool check = false;
    Byte* bytes = (Byte*)[netData.bodyData bytes];
    int result = bytes[0];
    NSDictionary* userData;
    GBInterMaster_Bean_DeviceInfo* bean = nil;
    NSMutableArray *devices = [NSMutableArray arrayWithCapacity:0];
    if(result == 0x00){
        if(netData.bodyData.length > 1){
            int i = 0;
            int size = bytes[1];
            bool isPossessor = false;
            bool isOnLine = false;
            int deviceType = 0;
            long timeCP = 0;
            NSString* deviceAccount = nil;
            NSString* deviceAlias = nil;
            int deviceAliasLen = 0;
            int offset = 2;
            bool isDevice = false;
            @try {
                for (i; i < size; ++i) {
                    deviceAccount = [[NSString alloc] initWithBytes:bytes+offset length:16 encoding:NSUTF8StringEncoding];
                    offset += 16;
                    isPossessor = bytes[offset] == 1 ? true : false;
                    offset += 1;
                    isOnLine = bytes[offset] == 1 ? true : false;
                    offset += 1;
                    deviceType = (bytes[offset] & 0x00FF) + ((bytes[offset + 1] & 0x00FF) << 8 & 0xFF00);
                    offset += 2;
                    deviceAliasLen = bytes[offset];
                    offset += 1;
                    deviceAlias = [[NSString alloc] initWithBytes:bytes+offset length:deviceAliasLen encoding:NSUTF8StringEncoding];
                    offset += deviceAliasLen;
                    timeCP = (long) ((bytes[offset] & 0x00FF)
                                     + ((bytes[offset + 1] & 0x00FF) << 8 & 0xFF00)
                                     + ((bytes[offset + 2] & 0x00FF) << 16 & 0xFF0000) + (((long)bytes[offset + 3] & 0x00FF) << 24 & 0xFF000000));
                    offset += 4;
                    isDevice = false;
                    if(deviceType == DEVICE_TYPE_AMPUX_GM8_13001){
                        isDevice = true;
                        bean = [[GBInterMaster_Bean_DeviceInfo13001 alloc] init];
                        GBInterMaster_Bean_DeviceInfo13001* tmpDevice = (GBInterMaster_Bean_DeviceInfo13001*)bean;
                        tmpDevice.currentRoomTempe = (bytes[offset] & 0x00FF) + ((bytes[offset + 1] & 0x00FF) << 8 & 0xFF00);
                        offset += 2;

                        tmpDevice.isHeat = bytes[offset] == 1 ? true : false;
                        offset += 1;
                        tmpDevice.heatMode = bytes[offset] == 1 ? true : false;
                        offset += 1;
                        tmpDevice.powerSwitch = bytes[offset];
                        offset += 1;
                        tmpDevice.isSiliconControlProtection = bytes[offset] == 1 ? true : false;
                        offset += 1;
                        tmpDevice.isOverTempProtection = bytes[offset] == 1 ? true : false;
                        offset += 1;
                        tmpDevice.isColdTempProtection = bytes[offset] == 1 ? true : false;
                        offset += 1;
                       
                    }
                    if(isDevice){
                        bean.isPossessor = isPossessor;
                        bean.isOnLine = isOnLine;
                        bean.deviceType = deviceType;
                        bean.account = deviceAccount;
                        bean.alias = deviceAlias;
                        bean.stateTime = timeCP;
                        bean.upLoadingTime = timeCP;
                        [devices  addObject:bean];
                    }
                }
                check = true;
            }
            @catch (NSException *exception) {
                check = false;
                
            }
        }
    }
    
    if(check){
        userData = @{@"msgNumber":[NSNumber numberWithInt:netData.msgId],@"responseCode":[NSNumber numberWithInt:result],@"devices":devices};
    }else{
        userData = @{@"msgNumber":[NSNumber numberWithInt:netData.msgId],@"responseCode":[NSNumber numberWithInt:1]};
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_RESPONSE_GET_DEVICE_LIST object:nil userInfo:userData];
    return result;
}


/* 获取设备使用者列表 */
+ (int) requestDeviceUserList:(int)msgNumber withDeviceAccount:(NSString*)deviceAccount withService:(GBInterMaster_Launcher*)service{
    NSData* account_dt = [deviceAccount dataUsingEncoding:NSUTF8StringEncoding];
    GBInterMaster_NetData* packData = [[GBInterMaster_NetData alloc] init];
    packData.cmdType = PTCP_V10_VALUE_CMD_TYPE_APP_SYSTEM;
    packData.cmd = LOGIC_APP_CMD_DEVICEUSERLIST;
    packData.connId = service.serverConnID;
    packData.msgId = msgNumber;
    Byte reqs[16];
    memset(reqs, 0x00, 16);
    memcpy(reqs, [account_dt bytes] ,account_dt.length < 16 ? account_dt.length:16 );
    packData.bodyData = [[NSData alloc] initWithBytes:reqs length:16];
    PTCP res = [GBInterMaster_PackUtil pack10:packData];
    if(res == PTCP_OK){
        [service.socket writeData:packData.packData withTimeout:SEND_MSG_OVER_TIME tag:12];
    }
    return 0;
}
/* 获取设备使用者列表反馈 */
+ (int) responseDeviceUserList:(GBInterMaster_NetData*)netData withService:(GBInterMaster_Launcher*)service{
    if (netData.bodyData.length < 17) {
        return 0x01;
    }
    int offset = 0;
    bool check = false;
    Byte* bytes = (Byte*)[netData.bodyData bytes];
    int result = bytes[offset];
    offset += 1;
    NSDictionary* userData;
    GBInterMaster_Bean_DeviceOperatorInfo* bean = nil;
    NSMutableArray *operators = [NSMutableArray arrayWithCapacity:0];
    if(result == 0x00){
        if(netData.bodyData.length > 1){
            int i = 0;
            NSString* deviceAccount = [[NSString alloc] initWithBytes:bytes+offset length:16 encoding:NSUTF8StringEncoding];
            offset += 16;
            if (netData.bodyData.length > 17) {
                int size = bytes[offset];
                offset += 1;
                int len = 0;
                @try {
                    for (i = 0; i < size; ++i) {
                        bean = [[GBInterMaster_Bean_DeviceOperatorInfo alloc] init];
                        bean.deviceAccount = deviceAccount;
                        len = bytes[offset];
                        offset += 1;
                        bean.userAccount = [[NSString alloc] initWithBytes:bytes+offset length:len encoding:NSUTF8StringEncoding];
                        offset += len;
                        len = bytes[offset];
                        len += 1;
                        bean.userAlias = [[NSString alloc] initWithBytes:bytes+offset length:len encoding:NSUTF8StringEncoding];
                        offset += len;
                        [operators  addObject:bean];
                        NSLog(@"offset:%d",offset);
                    }
                    if(netData.bodyData.length == offset){
                        check = true;
                    }
                }@catch (NSException *exception) {
                    check = false;
                }
            }
        }
    }
    if(check){
        userData = @{@"msgNumber":[NSNumber numberWithInt:netData.msgId],@"responseCode":[NSNumber numberWithInt:result],@"operators":operators};
    }else{
        userData = @{@"msgNumber":[NSNumber numberWithInt:netData.msgId],@"responseCode":[NSNumber numberWithInt:1]};
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_RESPONSE_GET_DEVICE_USERS_LIST object:nil userInfo:userData];
    return result;
}


/* 解除绑定 */
+ (int) requestRelieveDevice:(int)msgNumber withDeviceAccount:(NSString*)deviceAccount withService:(GBInterMaster_Launcher*)service{
    NSData* account_dt = [deviceAccount dataUsingEncoding:NSUTF8StringEncoding];
    GBInterMaster_NetData* packData = [[GBInterMaster_NetData alloc] init];
    packData.cmdType = PTCP_V10_VALUE_CMD_TYPE_APP_SYSTEM;
    packData.cmd = LOGIC_APP_CMD_REMOVEDEVICE;
    packData.connId = service.serverConnID;
    packData.msgId = msgNumber;
    Byte reqs[16];
    memset(reqs, 0x00, 16);
    memcpy(reqs, [account_dt bytes] ,account_dt.length < 16 ? account_dt.length:16 );
    packData.bodyData = [[NSData alloc] initWithBytes:reqs length:16];
    PTCP res = [GBInterMaster_PackUtil pack10:packData];
    if(res == PTCP_OK){
        [service.socket writeData:packData.packData withTimeout:SEND_MSG_OVER_TIME tag:13];
    }
    return 0;
}
/* 解除绑定反馈 */
+ (int) responseRelieveDevice:(GBInterMaster_NetData*)netData withService:(GBInterMaster_Launcher*)service{
    if (netData.bodyData.length != 17) {
        return 0x01;
    }
    Byte* bytes = (Byte*)[netData.bodyData bytes];
    int result = bytes[0];
    NSDictionary* userData = @{@"msgNumber":[NSNumber numberWithInt:netData.msgId],@"responseCode":[NSNumber numberWithInt:result]};
    [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_RESPONSE_RELIEVE_DEVICE object:nil userInfo:userData];
    return result;
}


/* 添加使用者 */
+ (int) requestAddDeviceUser:(int)msgNumber withDeviceAccount:(NSString*)deviceAccount withUserAccount:(NSString*)userAccount withUserAlias:(NSString*)userAlias withService:(GBInterMaster_Launcher*)service{
    NSData* device_account_dt = [deviceAccount dataUsingEncoding:NSUTF8StringEncoding];
    NSData* user_account_dt = [userAccount dataUsingEncoding:NSUTF8StringEncoding];
    NSData* user_alias_dt = [userAlias dataUsingEncoding:NSUTF8StringEncoding];
    unsigned long user_account_len = [user_account_dt length];
    unsigned long user_alias_len = [user_alias_dt length];
    unsigned long len = 18+user_account_len+user_alias_len;
    GBInterMaster_NetData* packData = [[GBInterMaster_NetData alloc] init];
    packData.cmdType = PTCP_V10_VALUE_CMD_TYPE_APP_SYSTEM;
    packData.cmd = LOGIC_APP_CMD_ADDDEVICEUSER;
    packData.connId = service.serverConnID;
    packData.msgId = msgNumber;
    int offset = 0;
    Byte reqs[len];
    memset(reqs, 0x00, len);
    memcpy(reqs, [device_account_dt bytes] , device_account_dt.length < 16 ? device_account_dt.length:16);
    offset += 16;
    reqs[offset] = user_account_len;
    offset += 1;
    memcpy(reqs+offset, [user_account_dt bytes] , user_account_len);
    offset += user_account_len;
    reqs[offset] = user_alias_len;
    offset += 1;
    memcpy(reqs+offset, [user_alias_dt bytes] , user_alias_len);
    offset += user_alias_len;
    packData.bodyData = [[NSData alloc] initWithBytes:reqs length:len];
    PTCP res = [GBInterMaster_PackUtil pack10:packData];
    if(res == PTCP_OK){
        [service.socket writeData:packData.packData withTimeout:SEND_MSG_OVER_TIME tag:14];
    }
    return 0;
    
}
/* 添加使用者 反馈 */
+ (int) responseAddDeviceUser:(GBInterMaster_NetData*)netData withService:(GBInterMaster_Launcher*)service{
    if (netData.bodyData.length != 17) {
        return 0x01;
    }
    Byte* bytes = (Byte*)[netData.bodyData bytes];
    int result = bytes[0];
    NSDictionary* userData = @{@"msgNumber":[NSNumber numberWithInt:netData.msgId],@"responseCode":[NSNumber numberWithInt:result]};
    [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_RESPONSE_ADD_DEVICE_USER object:nil userInfo:userData];
    return result;
}
/* 添加使用者 推送 */
+ (int) pushAddDeviceUser:(GBInterMaster_NetData*)netData withService:(GBInterMaster_Launcher*)service{
    return 0;
}

/* 删除使用者 */
+ (int) requestDeleteDeviceUser:(int)msgNumber withDeviceAccount:(NSString*)deviceAccount withUserAccount:(NSString*)userAccount  withService:(GBInterMaster_Launcher*)service{
    NSData* device_account_dt = [deviceAccount dataUsingEncoding:NSUTF8StringEncoding];
    NSData* user_account_dt = [userAccount dataUsingEncoding:NSUTF8StringEncoding];
    unsigned long user_account_len = [user_account_dt length];
    unsigned long len = 17+user_account_len;
    GBInterMaster_NetData* packData = [[GBInterMaster_NetData alloc] init];
    packData.cmdType = PTCP_V10_VALUE_CMD_TYPE_APP_SYSTEM;
    packData.cmd = LOGIC_APP_CMD_DELETEDEVICEUSER;
    packData.connId = service.serverConnID;
    packData.msgId = msgNumber;
    Byte reqs[len];
    memset(reqs, 0x00, len);
    memcpy(reqs, [device_account_dt bytes] , device_account_dt.length < 16 ? device_account_dt.length:16);
    reqs[16] = user_account_len;
    memcpy(reqs+17, [user_account_dt bytes] , user_account_len);
    packData.bodyData = [[NSData alloc] initWithBytes:reqs length:len];
    PTCP res = [GBInterMaster_PackUtil pack10:packData];
    if(res == PTCP_OK){
        [service.socket writeData:packData.packData withTimeout:SEND_MSG_OVER_TIME tag:15];
    }
    return 0;
}
/* 删除使用者反馈 */
+ (int) responseDeleteDeviceUser:(GBInterMaster_NetData*)netData withService:(GBInterMaster_Launcher*)service{
    if (netData.bodyData.length != 17) {
        return 0x01;
    }
    Byte* bytes = (Byte*)[netData.bodyData bytes];
    int result = bytes[0];
    NSDictionary* userData = @{@"msgNumber":[NSNumber numberWithInt:netData.msgId],@"responseCode":[NSNumber numberWithInt:result]};
    [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_RESPONSE_DELETE_DEVICE_USER object:nil userInfo:userData];
    return result;
}
/* 删除使用者反馈 推送 */
+ (int) pushDeleteDeviceUser:(GBInterMaster_NetData*)netData withService:(GBInterMaster_Launcher*)service{
    return 0;
}


/* 编辑使用者 */
+ (int) requestEditDeviceUser:(int)msgNumber withDeviceAccount:(NSString*)deviceAccount withUserAccount:(NSString*)userAccount withUserAlias:(NSString*)userAlias withService:(GBInterMaster_Launcher*)service{
    NSData* device_account_dt = [deviceAccount dataUsingEncoding:NSUTF8StringEncoding];
    NSData* user_account_dt = [userAccount dataUsingEncoding:NSUTF8StringEncoding];
    NSData* user_alias_dt = [userAlias dataUsingEncoding:NSUTF8StringEncoding];
    unsigned long user_account_len = [user_account_dt length];
    unsigned long user_alias_len = [user_alias_dt length];
    unsigned long len = 18+user_account_len+user_alias_len;
    GBInterMaster_NetData* packData = [[GBInterMaster_NetData alloc] init];
    packData.cmdType = PTCP_V10_VALUE_CMD_TYPE_APP_SYSTEM;
    packData.cmd = LOGIC_APP_CMD_UPDATEDEVICEUSER;
    packData.connId = service.serverConnID;
    packData.msgId = msgNumber;
    int offset = 0;
    Byte reqs[len];
    memset(reqs, 0x00, len);
    memcpy(reqs, [device_account_dt bytes] , device_account_dt.length < 16 ? device_account_dt.length:16);
    offset += 16;
    reqs[offset] = user_account_len;
    offset += 1;
    memcpy(reqs+offset, [user_account_dt bytes] , user_account_len);
    offset += user_account_len;
    reqs[offset] = user_alias_len;
    offset += 1;
    memcpy(reqs+offset, [user_alias_dt bytes] , user_alias_len);
    offset += user_alias_len;
    packData.bodyData = [[NSData alloc] initWithBytes:reqs length:len];
    PTCP res = [GBInterMaster_PackUtil pack10:packData];
    if(res == PTCP_OK){
        [service.socket writeData:packData.packData withTimeout:SEND_MSG_OVER_TIME tag:16];
    }
    return 0;
}
/* 编辑使用者 反馈 */
+ (int) responseEditDeviceUser:(GBInterMaster_NetData*)netData withService:(GBInterMaster_Launcher*)service{
    if (netData.bodyData.length != 17) {
        return 0x01;
    }
    Byte* bytes = (Byte*)[netData.bodyData bytes];
    int result = bytes[0];
    NSDictionary* userData = @{@"msgNumber":[NSNumber numberWithInt:netData.msgId],@"responseCode":[NSNumber numberWithInt:result]};
    [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_RESPONSE_EDIT_DEVICE_USER object:nil userInfo:userData];
    return result;
}


/* 所有者转移 */
+ (int) requestDeviceOwnChange:(int)msgNumber withDeviceAccount:(NSString*)deviceAccount withUserAccount:(NSString*)userAccount  withService:(GBInterMaster_Launcher*)service{
    NSData* device_account_dt = [deviceAccount dataUsingEncoding:NSUTF8StringEncoding];
    NSData* user_account_dt = [userAccount dataUsingEncoding:NSUTF8StringEncoding];
    unsigned long user_account_len = [user_account_dt length];
    unsigned long len = 17+user_account_len;
    GBInterMaster_NetData* packData = [[GBInterMaster_NetData alloc] init];
    packData.cmdType = PTCP_V10_VALUE_CMD_TYPE_APP_SYSTEM;
    packData.cmd = LOGIC_APP_CMD_DEVICEOWNCHANGE;
    packData.connId = service.serverConnID;
    packData.msgId = msgNumber;
    Byte reqs[len];
    memset(reqs, 0x00, len);
    memcpy(reqs, [device_account_dt bytes] , device_account_dt.length < 16 ? device_account_dt.length:16);
    reqs[16] = user_account_len;
    memcpy(reqs+17, [user_account_dt bytes] , user_account_len);
    packData.bodyData = [[NSData alloc] initWithBytes:reqs length:len];
    PTCP res = [GBInterMaster_PackUtil pack10:packData];
    if(res == PTCP_OK){
        [service.socket writeData:packData.packData withTimeout:SEND_MSG_OVER_TIME tag:17];
    }
    return 0;
}
/* 所有者转移反馈 */
+ (int) responseDeviceOwnChange:(GBInterMaster_NetData*)netData withService:(GBInterMaster_Launcher*)service{
    if (netData.bodyData.length != 17) {
        return 0x01;
    }
    Byte* bytes = (Byte*)[netData.bodyData bytes];
    int result = bytes[0];
    NSDictionary* userData = @{@"msgNumber":[NSNumber numberWithInt:netData.msgId],@"responseCode":[NSNumber numberWithInt:result]};
    [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_RESPONSE_CHANGE_DEVICE_OWN object:nil userInfo:userData];
    return result;
}
/* 所有者转移 推送 */
+ (int) pushDeviceOwnChange:(GBInterMaster_NetData*)netData withService:(GBInterMaster_Launcher*)service{
    return 0;
}


/* 用户意见 */
+ (int) requestUserFeedBack:(int)msgNumber withTheme:(NSString*)theme withContent:(NSString*)content withService:(GBInterMaster_Launcher*)service{
    NSData* theme_dt = [theme dataUsingEncoding:NSUTF8StringEncoding];
    NSData* content_dt = [content dataUsingEncoding:NSUTF8StringEncoding];
    unsigned long theme_len = [theme_dt length];
    unsigned long content_len = [content_dt length];
    unsigned long len = 2+theme_len+content_len;
    GBInterMaster_NetData* packData = [[GBInterMaster_NetData alloc] init];
    packData.cmdType = PTCP_V10_VALUE_CMD_TYPE_APP_SYSTEM;
    packData.cmd = LOGIC_APP_CMD_REQUESTUSERFEEDBACK;
    packData.connId = service.serverConnID;
    packData.msgId = msgNumber;
    int offset = 0;
    Byte reqs[len];
    memset(reqs, 0x00, len);
    reqs[offset] = theme_len;
    offset += 1;
    memcpy(reqs+offset, [theme_dt bytes] , theme_len);
    offset += theme_len;
    reqs[offset] = content_len;
    offset += 1;
    memcpy(reqs+offset, [content_dt bytes] , content_len);
    offset += content_len;
    packData.bodyData = [[NSData alloc] initWithBytes:reqs length:len];
    PTCP res = [GBInterMaster_PackUtil pack10:packData];
    if(res == PTCP_OK){
        [service.socket writeData:packData.packData withTimeout:SEND_MSG_OVER_TIME tag:18];
    }
    return 0;
}
/* 用户意见反馈 */
+ (int) responseUserFeedBack:(GBInterMaster_NetData*)netData withService:(GBInterMaster_Launcher*)service{
    return 0;
}


/* 获取设备配置 */
+ (int) requestDeviceConfig:(int)msgNumber withDeviceAccount:(NSString*)deviceAccount withService:(GBInterMaster_Launcher*)service{
    NSData* account_dt = [deviceAccount dataUsingEncoding:NSUTF8StringEncoding];
    GBInterMaster_NetData* packData = [[GBInterMaster_NetData alloc] init];
    packData.cmdType = PTCP_V10_VALUE_CMD_TYPE_APP_SYSTEM;
    packData.cmd = LOGIC_APP_CMD_GETDEVICECONFIG;
    packData.connId = service.serverConnID;
    packData.msgId = msgNumber;
    Byte reqs[16];
    memset(reqs, 0x00, 16);
    memcpy(reqs, [account_dt bytes] ,account_dt.length < 16 ? account_dt.length:16 );
    packData.bodyData = [[NSData alloc] initWithBytes:reqs length:16];
    PTCP res = [GBInterMaster_PackUtil pack10:packData];
    if(res == PTCP_OK){
        [service.socket writeData:packData.packData withTimeout:SEND_MSG_OVER_TIME tag:19];
    }
    return 0;
    
}
/* 获取设备配置反馈 */
+ (int) responseDeviceConfig:(GBInterMaster_NetData*)netData withService:(GBInterMaster_Launcher*)service{
    if (netData.bodyData.length < 19) {
        return 0x01;
    }
    Byte* bytes = (Byte*)[netData.bodyData bytes];
    int result = bytes[0];
    NSString* deviceType = [[NSString alloc] initWithBytes:bytes+1 length:3 encoding:NSUTF8StringEncoding];
    int device_type = [GBInterMaster_Util convert36to10:deviceType withLength:3];
    if(device_type == DEVICE_TYPE_AMPUX_GM8_13001){
        result = [GBInterMaster_DeviceLogic13001 responseDeviceConfig:netData];
    }
    return result;
}


/* 消息同步设置 */
+ (int) requestMsgReceiveMode:(int)msgNumber withDeviceAccount:(NSString*)deviceAccount withMsgMode:(int) msgMode withService:(GBInterMaster_Launcher*)service{
    NSData* account_dt = [deviceAccount dataUsingEncoding:NSUTF8StringEncoding];
    GBInterMaster_NetData* packData = [[GBInterMaster_NetData alloc] init];
    packData.cmdType = PTCP_V10_VALUE_CMD_TYPE_APP_SYSTEM;
    packData.cmd = LOGIC_APP_CMD_MSGRECEIVEMODEL;
    packData.connId = service.serverConnID;
    packData.msgId = msgNumber;
    Byte reqs[17];
    memset(reqs, 0x00, 17);
    memcpy(reqs, [account_dt bytes] ,account_dt.length < 16 ? account_dt.length:16 );
    reqs[16] = msgMode;
    packData.bodyData = [[NSData alloc] initWithBytes:reqs length:17];
    PTCP res = [GBInterMaster_PackUtil pack10:packData];
    if(res == PTCP_OK){
        [service.socket writeData:packData.packData withTimeout:SEND_MSG_OVER_TIME tag:20];
    }
    return 0;
}
/* 消息同步设置反馈 */
+ (int) responseMsgReceiveMode:(GBInterMaster_NetData*)netData withService:(GBInterMaster_Launcher*)service{
    if (netData.bodyData.length != 1) {
        return 0x01;
    }
    Byte* bytes = (Byte*)[netData.bodyData bytes];
    int result = bytes[0];
    NSDictionary* userData = @{@"msgNumber":[NSNumber numberWithInt:netData.msgId],@"responseCode":[NSNumber numberWithInt:result]};
    [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_RESPONSE_MSG_RECEIVE_MODE object:nil userInfo:userData];
    return result;
}




/* 获取用户操作记录 */
+ (int) requestUserOpes:(int)msgNumber withTag:(int)tag withDeviceAccount:(NSString*)deviceAccount withUserAccount:(NSString*)userAccount  withService:(GBInterMaster_Launcher*)service{
    NSData* device_account_dt = [deviceAccount dataUsingEncoding:NSUTF8StringEncoding];
    NSData* user_account_dt = [userAccount dataUsingEncoding:NSUTF8StringEncoding];
    unsigned long user_account_len = [user_account_dt length];
    unsigned long len = 18+user_account_len;
    GBInterMaster_NetData* packData = [[GBInterMaster_NetData alloc] init];
    packData.cmdType = PTCP_V10_VALUE_CMD_TYPE_APP_SYSTEM;
    packData.cmd = LOGIC_APP_CMD_GETUSEROPES;
    packData.connId = service.serverConnID;
    packData.msgId = msgNumber;
    int offset = 0;
    Byte reqs[len];
    memset(reqs, 0x00, len);
    memcpy(reqs, [device_account_dt bytes] , device_account_dt.length < 16 ? device_account_dt.length:16);
    offset += 16;
    reqs[offset] = user_account_len;
    offset += 1;
    memcpy(reqs+offset, [user_account_dt bytes] , user_account_len);
    offset += user_account_len;
    reqs[offset] = tag;
    offset += 1;
    packData.bodyData = [[NSData alloc] initWithBytes:reqs length:len];
    PTCP res = [GBInterMaster_PackUtil pack10:packData];
    if(res == PTCP_OK){
        [service.socket writeData:packData.packData withTimeout:SEND_MSG_OVER_TIME tag:21];
    }
    return 0;
}
/* 获取用户操作记录反馈 */
+ (int) responseUserOpes:(GBInterMaster_NetData*)netData withService:(GBInterMaster_Launcher*)servic
{
    if (netData.bodyData.length < 17) {
        return 0x01;
    }
    Byte* bytes = (Byte*)[netData.bodyData bytes];
    int result = bytes[0];
    NSString* deviceType = [[NSString alloc] initWithBytes:bytes+1 length:3 encoding:NSUTF8StringEncoding];
    int device_type = [GBInterMaster_Util convert36to10:deviceType withLength:3];
    if(device_type == DEVICE_TYPE_AMPUX_GM8_13001){
        result = [GBInterMaster_DeviceLogic13001 responseDeviceUserOpes:netData];
    }
    return result;
}

/* 获取设备最新状态 */
+ (int) requestDeviceStatus:(int)msgNumber withDataType:(int)dataType withSubId:(int)subId withDeviceAccount:(NSString*)deviceAccount withService:(GBInterMaster_Launcher*)service{
    NSData* account_dt = [deviceAccount dataUsingEncoding:NSUTF8StringEncoding];
    GBInterMaster_NetData* packData = [[GBInterMaster_NetData alloc] init];
    packData.cmdType = PTCP_V10_VALUE_CMD_TYPE_APP_SYSTEM;
    packData.cmd = LOGIC_APP_CMD_GETDEVICESTATUS;
    packData.connId = service.serverConnID;
    packData.msgId = msgNumber;
    if(dataType == PTCP_V10_DATA_TYPE_SUB){
        Byte reqs[17];
        memset(reqs, 0x00, 17);
        memcpy(reqs, [account_dt bytes] ,account_dt.length < 16 ? account_dt.length:16 );
        reqs[16] = subId;
        packData.bodyData = [[NSData alloc] initWithBytes:reqs length:17];
    }else{
        Byte reqs[16];
        memset(reqs, 0x00, 16);
        memcpy(reqs, [account_dt bytes] ,account_dt.length < 16 ? account_dt.length:16 );
        packData.bodyData = [[NSData alloc] initWithBytes:reqs length:16];
    }
    PTCP res = [GBInterMaster_PackUtil pack10:packData];
    if(res == PTCP_OK){
        [service.socket writeData:packData.packData withTimeout:SEND_MSG_OVER_TIME tag:26];
    }
    return 0;
}
/* 获取设备最新状态反馈 */
+ (int) responseDeviceStatus:(GBInterMaster_NetData*)netData withService:(GBInterMaster_Launcher*)service{
    if (netData.bodyData.length < 17) {
        return 0x01;
    }
    Byte* bytes = (Byte*)[netData.bodyData bytes];
    int result = bytes[0];
    NSString* deviceType = [[NSString alloc] initWithBytes:bytes+1 length:3 encoding:NSUTF8StringEncoding];
    int device_type = [GBInterMaster_Util convert36to10:deviceType withLength:3];
    if(device_type == DEVICE_TYPE_AMPUX_GM8_13001){
        result = [GBInterMaster_DeviceLogic13001 reponseRunningState:netData];
    }
    return result;
}


/* 获取设备运行记录 */
+ (int) requestDeviceRuns:(int)msgNumber withDataType:(int)dataType withSubId:(int)subId withTag:(int)tag withDeviceAccount:(NSString*)deviceAccount withService:(GBInterMaster_Launcher*)service{
    NSData* account_dt = [deviceAccount dataUsingEncoding:NSUTF8StringEncoding];
    GBInterMaster_NetData* packData = [[GBInterMaster_NetData alloc] init];
    packData.cmdType = PTCP_V10_VALUE_CMD_TYPE_APP_SYSTEM;
    packData.cmd = LOGIC_APP_CMD_GETDEVICERUNRECORD;
    packData.connId = service.serverConnID;
    packData.msgId = msgNumber;
    if(dataType == PTCP_V10_DATA_TYPE_SUB){
        Byte reqs[18];
        memset(reqs, 0x00, 18);
        memcpy(reqs, [account_dt bytes] ,account_dt.length < 16 ? account_dt.length:16 );
        reqs[16] = subId;
        reqs[17] = tag;
        packData.bodyData = [[NSData alloc] initWithBytes:reqs length:18];
    }else{
        Byte reqs[17];
        memset(reqs, 0x00, 17);
        memcpy(reqs, [account_dt bytes] ,account_dt.length < 16 ? account_dt.length:16 );
        reqs[16] = tag;
        packData.bodyData = [[NSData alloc] initWithBytes:reqs length:17];
    }
    PTCP res = [GBInterMaster_PackUtil pack10:packData];
    if(res == PTCP_OK){
        [service.socket writeData:packData.packData withTimeout:SEND_MSG_OVER_TIME tag:22];
    }
    return 0;
}
/* 获取设备运行记录反馈 */
+ (int) responseDeviceRuns:(GBInterMaster_NetData*)netData withService:(GBInterMaster_Launcher*)service{
    if (netData.bodyData.length < 17) {
        return 0x01;
    }
    Byte* bytes = (Byte*)[netData.bodyData bytes];
    int result = bytes[0];
    NSString* deviceType = [[NSString alloc] initWithBytes:bytes+1 length:3 encoding:NSUTF8StringEncoding];
    int device_type = [GBInterMaster_Util convert36to10:deviceType withLength:3];
    if(device_type == DEVICE_TYPE_AMPUX_GM8_13001){
        result = [GBInterMaster_DeviceLogic13001 responseDeviceRunRecord:netData];
    }
    return result;
}


/* 获取时段信息 */
+ (int) requestShowProgramInfo:(int)msgNumber withDataType:(int)dataType withSubId:(int)subId withDeviceAccount:(NSString*)deviceAccount withService:(GBInterMaster_Launcher*)service{
    NSData* account_dt = [deviceAccount dataUsingEncoding:NSUTF8StringEncoding];
    GBInterMaster_NetData* packData = [[GBInterMaster_NetData alloc] init];
    packData.cmdType = PTCP_V10_VALUE_CMD_TYPE_APP_SYSTEM;
    packData.cmd = LOGIC_APP_CMD_GETPROGRAMINFO;
    packData.connId = service.serverConnID;
    packData.msgId = msgNumber;
    if(dataType == PTCP_V10_DATA_TYPE_SUB){
        Byte reqs[17];
        memset(reqs, 0x00, 17);
        memcpy(reqs, [account_dt bytes] ,account_dt.length < 16 ? account_dt.length:16 );
        reqs[16] = subId;
        packData.bodyData = [[NSData alloc] initWithBytes:reqs length:17];
    }else{
        Byte reqs[16];
        memset(reqs, 0x00, 16);
        memcpy(reqs, [account_dt bytes] ,account_dt.length < 16 ? account_dt.length:16 );
        packData.bodyData = [[NSData alloc] initWithBytes:reqs length:16];
    }
    PTCP res = [GBInterMaster_PackUtil pack10:packData];
    if(res == PTCP_OK){
        [service.socket writeData:packData.packData withTimeout:SEND_MSG_OVER_TIME tag:23];
    }
    return 0;
}
/* 获取时段信息反馈 */
+ (int) responseShowProgramInfo:(GBInterMaster_NetData*)netData withService:(GBInterMaster_Launcher*)service{
    if (netData.bodyData.length < 17) {
        return 0x01;
    }
    Byte* bytes = (Byte*)[netData.bodyData bytes];
    int result = bytes[0];
    NSString* deviceType = [[NSString alloc] initWithBytes:bytes+1 length:3 encoding:NSUTF8StringEncoding];
    int device_type = [GBInterMaster_Util convert36to10:deviceType withLength:3];
    if(device_type == DEVICE_TYPE_AMPUX_GM8_13001){
        result = [GBInterMaster_DeviceLogic13001 responseShowProgramInfo:netData];
    }
    return result;
}

/* 获取设备参数 */
+ (int) requestDeviceParam:(int)msgNumber withDataType:(int)dataType withSubId:(int)subId withDeviceAccount:(NSString*)deviceAccount withType:(int)type withService:(GBInterMaster_Launcher*)service{
    NSData* account_dt = [deviceAccount dataUsingEncoding:NSUTF8StringEncoding];
    GBInterMaster_NetData* packData = [[GBInterMaster_NetData alloc] init];
    packData.cmdType = PTCP_V10_VALUE_CMD_TYPE_APP_SYSTEM;
    packData.cmd = LOGIC_APP_CMD_GETDEVICEPARAM;
    packData.connId = service.serverConnID;
    packData.msgId = msgNumber;
    if(dataType == PTCP_V10_DATA_TYPE_SUB){
        Byte reqs[18];
        memset(reqs, 0x00, 18);
        memcpy(reqs, [account_dt bytes] ,account_dt.length < 16 ? account_dt.length:16 );
        reqs[16] = subId;
        reqs[17] = type;
        packData.bodyData = [[NSData alloc] initWithBytes:reqs length:18];
    }else{
        Byte reqs[17];
        memset(reqs, 0x00, 17);
        memcpy(reqs, [account_dt bytes] ,account_dt.length < 16 ? account_dt.length:16 );
        reqs[16] = type;
        packData.bodyData = [[NSData alloc] initWithBytes:reqs length:17];
    }
    PTCP res = [GBInterMaster_PackUtil pack10:packData];
    if(res == PTCP_OK){
        [service.socket writeData:packData.packData withTimeout:SEND_MSG_OVER_TIME tag:24];
    }
    return 0;
}
/* 获取设备参数反馈 */
+ (int) responseDeviceParam:(GBInterMaster_NetData*)netData withService:(GBInterMaster_Launcher*)service{
    return 0;
}


@end
